package com.example.pharmacy.controller;

import com.example.pharmacy.commen.Constant;
import com.example.pharmacy.model.OrderInfo;
import com.example.pharmacy.model.OrderResult;
import com.example.pharmacy.model.Result;
import com.example.pharmacy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-21
 * Time: 20:07
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/getOrders")
    public Result getOrders(HttpSession session) {
        String name = (String) session.getAttribute(Constant.SESSION_USER_KEY);
        List<OrderResult> orderResults = orderService.getOrdersByname(name);
        if (orderResults == null) {
            return Result.fail("查找历史订单失败");
        }
        return Result.success(orderResults);
    }

    @RequestMapping("/updateOrder")
    public Result updateOrder(OrderInfo orderInfo) {
        if (orderInfo == null) {
            return Result.fail("订单信息为空!");
        }
        if (orderInfo.getOrderId() == null || orderInfo.getOrderId() <= 0) {
            return Result.fail("订单信息有误!");
        }
        Integer ret = orderService.updateOrder(orderInfo);
        if (ret == 0) {
            return Result.fail("订单修改失败");
        }
        return Result.success("");
    }
}
